﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WXYW.Entities;
using WXYW.DAL;

namespace WXYW.BLL
{
    public class SparePartApplyService : RepositoryBase
    {
        public int GetNumberOfSparePartApplyForms(string code)
        {
            return GetEntities<SparePartApplyForm>(s => s.Code.Contains(code)).Count();
        }

        public List<SparePartApplyForm> GetAll(string keyword)
        {
            IQueryable<SparePartApplyForm> list = GetEntities<SparePartApplyForm>();
            if (!string.IsNullOrEmpty(keyword))
            {
                list = list.Where(s => s.ApplicationProduct.ApplicationForm.ApplicationCode.Contains(keyword) || s.ApplicationProduct.ProductType.ProductTypeName.Contains(keyword) || s.ServiceStation.Name.Contains(keyword));
            }
            foreach (SparePartApplyForm form in list)
            {
                if (!form.ServiceStationReference.IsLoaded)
                {
                    form.ServiceStationReference.Load();
                }
                if (!form.ApplicationProductReference.IsLoaded)
                {
                    form.ApplicationProductReference.Load();
                }
                if (!form.ApplicationProduct.ProductTypeReference.IsLoaded)
                {
                    form.ApplicationProduct.ProductTypeReference.Load();
                }
                if (!form.ApplicationProduct.ApplicationFormReference.IsLoaded)
                {
                    form.ApplicationProduct.ApplicationFormReference.Load();
                }
                if (!form.UserReference.IsLoaded)
                {
                    form.UserReference.Load();
                }
            }

            return list.ToList();
        }

        public List<SparePartApplyForm> GetSparePartApplyByStatus(string status)
        {
            List<SparePartApplyForm> list = GetEntities<SparePartApplyForm>(s => s.Status == status).ToList();
            foreach (SparePartApplyForm form in list)
            {
                if (!form.ServiceStationReference.IsLoaded)
                {
                    form.ServiceStationReference.Load();
                }
                if (!form.ApplicationProductReference.IsLoaded)
                {
                    form.ApplicationProductReference.Load();
                }
                if (!form.ApplicationProduct.ProductTypeReference.IsLoaded)
                {
                    form.ApplicationProduct.ProductTypeReference.Load();
                }
                if (!form.ApplicationProduct.ApplicationFormReference.IsLoaded)
                {
                    form.ApplicationProduct.ApplicationFormReference.Load();
                }
                if (!form.ApplicationProduct.UserReference.IsLoaded)
                {
                    form.ApplicationProduct.UserReference.Load();
                }
            }

            return list;
        }

        public List<SparePartApplyForm> GetSparePartApplyByServiceStation(int stationID)
        {
            List<SparePartApplyForm> list = GetEntities<SparePartApplyForm>(s => s.ServiceStation.ID == stationID).ToList();
            foreach (SparePartApplyForm form in list)
            {
                if (!form.ServiceStationReference.IsLoaded)
                {
                    form.ServiceStationReference.Load();
                }
                if (!form.ApplicationProductReference.IsLoaded)
                {
                    form.ApplicationProductReference.Load();
                }
                if (!form.ApplicationProduct.ProductTypeReference.IsLoaded)
                {
                    form.ApplicationProduct.ProductTypeReference.Load();
                }
            }

            return list;
        }


        public SparePartApplyForm GetSparePartApplyFormByID(long ID)
        {
            return GetEntities<SparePartApplyForm>(s => s.ID == ID).FirstOrDefault();
        }

        public SparePartApplyForm GetProductDetailForm(long spaID)
        {
            SparePartApplyForm spaForm = GetSparePartApplyFormByID(spaID);
            if (!spaForm.ApplicationProductReference.IsLoaded)
            {
                spaForm.ApplicationProductReference.Load();
            }
            if (!spaForm.ApplicationProduct.ProblemTypeReference.IsLoaded)
            {
                spaForm.ApplicationProduct.ProblemTypeReference.Load();
            }
            if (!spaForm.ApplicationProduct.ProductTypeReference.IsLoaded)
            {
                spaForm.ApplicationProduct.ProductTypeReference.Load();
            }
            if (!spaForm.ServiceStationReference.IsLoaded)
            {
                spaForm.ServiceStationReference.Load();
            }
            if (!spaForm.ApplicationProduct.ApplicationFormReference.IsLoaded)
            {
                spaForm.ApplicationProduct.ApplicationFormReference.Load();
            }
            if (!spaForm.UserReference.IsLoaded)
            {
                spaForm.UserReference.Load();
            }

            return spaForm;
        }

        public void AddSparePartApplyForm(string applicationProductID, string reason, long applicantID, string checkResult)
        {
            UserService userService = new UserService();
            User applicant = userService.GetUserByID(applicantID);
            if (!applicant.ServiceStationReference.IsLoaded)
            {
                applicant.ServiceStationReference.Load();
            }

            ProductService productService = new ProductService();
            ApplicationProduct product = productService.GetProductByID(long.Parse(applicationProductID));
            product.Status = "备件申请中";
            product.Remark = checkResult;

            SparePartApplyForm form = new SparePartApplyForm();
            form.ApplicationProduct = product;
            form.ApplyTime = DateTime.Now;
            form.Code = RunningNumberService.GenerateBJRunningNumber(applicant.ServiceStation.ID);
            form.User = applicant;
            form.Status = "备件-待审批";
            form.ServiceStation = applicant.ServiceStation;
            form.Reason = reason;

            InsertEntity(form);
        }


        public void Update(SparePartApplyForm form)
        {
            UpdateEntity(form);
        }

        public SparePartApplyForm GetSparePartFormApplicant(long p)
        {
            SparePartApplyForm form = GetSparePartApplyFormByID(p);
            if (!form.UserReference.IsLoaded)
            {
                form.UserReference.Load();
            }

            return form;
        }

        public IQueryable<SparePartApplyForm> GetSparePartApplyFormByApplicant(long userID, string keyword)
        {
            IQueryable<SparePartApplyForm> list = GetEntities<SparePartApplyForm>(s => s.User.UserID == userID);
            if (!string.IsNullOrEmpty(keyword))
            {
                list = list.Where(s => s.ApplicationProduct.ApplicationForm.ApplicationCode.Contains(keyword) || s.ApplicationProduct.ProductType.ProductTypeName.Contains(keyword) || s.ServiceStation.Name.Contains(keyword));
            }
            foreach (SparePartApplyForm form in list)
            {
                if (!form.ServiceStationReference.IsLoaded)
                {
                    form.ServiceStationReference.Load();
                }
                if (!form.ApplicationProductReference.IsLoaded)
                {
                    form.ApplicationProductReference.Load();
                }
                if (!form.ApplicationProduct.ProductTypeReference.IsLoaded)
                {
                    form.ApplicationProduct.ProductTypeReference.Load();
                }
                if (!form.ApplicationProduct.ApplicationFormReference.IsLoaded)
                {
                    form.ApplicationProduct.ApplicationFormReference.Load();
                }
                if (!form.UserReference.IsLoaded)
                {
                    form.UserReference.Load();
                }
            }
            return list;
        }

        public List<SparePartApplyForm> GetUpgrateFoemByKeywords(string keyword)
        {
            List<SparePartApplyForm> sparePartForms = new List<SparePartApplyForm>();
            if (!string.IsNullOrEmpty(keyword))
            {
                sparePartForms = GetEntities<SparePartApplyForm>(u => u.ServiceStation.Name.Contains(keyword)
                    || u.ApplicationProduct.ApplicationForm.ApplicationCode.Contains(keyword)
                    || u.ApplicationProduct.ProductType.ProductTypeName.Contains(keyword)
                    || u.ApplicationProduct.ProductSerialNumber.Contains(keyword)
                    || u.ApplicationProduct.RepairAccessory.Contains(keyword)
                    || u.ApplicationProduct.ProblemType.Name.Contains(keyword)
                    ).OrderByDescending(u => u.ApplyTime).ToList();
            }
            else
            {
                sparePartForms = GetEntities<SparePartApplyForm>().OrderByDescending(u => u.ApplyTime).ToList();
            }

            foreach (SparePartApplyForm form in sparePartForms)
            {
                if (!form.ServiceStationReference.IsLoaded)
                {
                    form.ServiceStationReference.Load();
                }
                if (!form.ApplicationProductReference.IsLoaded)
                {
                    form.ApplicationProductReference.Load();
                }
                if (!form.ApplicationProduct.ApplicationFormReference.IsLoaded)
                {
                    form.ApplicationProduct.ApplicationFormReference.Load();
                }
                if (!form.ApplicationProduct.ProductTypeReference.IsLoaded)
                {
                    form.ApplicationProduct.ProductTypeReference.Load();
                }
                if (!form.ApplicationProduct.ProblemTypeReference.IsLoaded)
                {
                    form.ApplicationProduct.ProblemTypeReference.Load();
                }
            }

            return sparePartForms;
        }

        public List<SparePartApplyForm> GetSparePartApplyByServiceStationAndStatus(int serviceStation, string status)
        {
            List<SparePartApplyForm> forms = GetEntities<SparePartApplyForm>(s => s.ServiceStation.ID == serviceStation && s.Status == status).ToList();
            foreach (SparePartApplyForm sform in forms)
            {
                if (!sform.ApplicationProductReference.IsLoaded)
                {
                    sform.ApplicationProductReference.Load();
                }
                if (!sform.ApplicationProduct.ProductTypeReference.IsLoaded)
                {
                    sform.ApplicationProduct.ProductTypeReference.Load();
                }
                if (!sform.ServiceStationReference.IsLoaded)
                {
                    sform.ServiceStationReference.Load();
                }
            }

            return forms;
        }

        public List<SparePartApplyForm> GetSparePartApplyByApplicantAndStatus(long userID, string status)
        {
            List<SparePartApplyForm> forms = GetEntities<SparePartApplyForm>(s => s.User.UserID == userID && s.Status == status).ToList();
            foreach (SparePartApplyForm sform in forms)
            {
                if (!sform.ApplicationProductReference.IsLoaded)
                {
                    sform.ApplicationProductReference.Load();
                }
                if (!sform.ApplicationProduct.ProductTypeReference.IsLoaded)
                {
                    sform.ApplicationProduct.ProductTypeReference.Load();
                }
                if (!sform.ServiceStationReference.IsLoaded)
                {
                    sform.ServiceStationReference.Load();
                }
            }

            return forms;
        }

        public SparePartApplyForm GetSparePartApplyByApplicationProductID(long productID)
        {
            return GetEntities<SparePartApplyForm>(s => s.ApplicationProduct.ApplicationProductID == productID).Where(s => s.Status != "备件-不同意").FirstOrDefault();
        }
    }
}
